<?php
namespace app\admin\controller;
use think\Controller;

Class Nav extends BaseAdmin
{

	//品牌列表
    public function lst()
    {
    	
        if(request()->isPost()){
            $data=input('post.');
            // dump($data);die;
            $nav=db('tp_nav');
            foreach ($data['sort'] as $k => $v) {
                $nav->where(['id'=>$k])->update(['sort'=>$v]);
            }
            $this->success('排序成功', 'lst');

        }
        $NavRes=db('tp_nav')->order('sort DESC')->paginate(5);
        $this->assign('NavRes',$NavRes);
        // dump($NavRes);
        // exit;
        return $this->fetch('list');
    }

    //品牌添加
    public function add()
    {
    	 
        if(request()->isPost()){
        	$data=input('post.');
        	if($data['nav_url']&&stripos($data['nav_url'],'http://')===false){
        		$data['nav_url']='http://'.$data['nav_url'];
        	}

        	//验证
        	// $validate = new \app\admin\validate\Nav;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=db('tp_nav')->insert($data);
        	if($add){
        		$this->success('保存成功','add');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }

    //品牌编辑
    public function edit($id)
    {
    	$NavRes=db('tp_nav')->where(array('id'=>$id))->find();
    	$this->assign('NavRes',$NavRes);
    	
        if(request()->isPost()){
        	$data=input('post.');
        	if($data['nav_url']&&stripos($data['nav_url'],'http://')===false){
        		$data['nav_url']='http://'.$data['nav_url'];
        	}
        	$up=db('tp_nav')->where(array('id'=>$id))->update($data);
        	// dump($up);
        	// exit;
        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }

    //品牌删除
    public function del($id)
    {
    	$del=db('tp_nav')->delete($id);
    	if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }


}